const parser = require("@babel/parser").parse;
const traverse = require("@babel/traverse").default;
const generate = require("@babel/generator").default
const t = require("@babel/types");

//  导入Webpack
const fs = require('fs')
// 改成我们自己抠的webpack代码
const code = fs.readFileSync('./21 - 联通沃邮箱登陆密码JS加密讲解.js', 'utf-8');

//  存放arr(Webpack 模块数组)
var arr = [ '437', 596, 597, 598 ];
!function () {
    var arr_ = []
    for (var item of arr) {
        arr_.push(item + "")
    }
    arr = arr_
}()


var ast = parser(code, {sourceType: "module"})

function NAME(value, name) {
    if (value !== undefined) {
        return value
    }
    return name
};
var Fun = {
    ObjectProperty(path) {
        let node = path.node
        // console.log(node.key.name, node.key.value)
        if ((node.key.value !== undefined || node.key.name !== undefined) && path.parent.type === "ObjectExpression") {
            if (path.parentPath.parent.type === "CallExpression" && path.parentPath.parentPath.parent.type === "UnaryExpression") {
                name = NAME(node.key.value, node.key.name)
                if (arr.indexOf(name.toString()) === -1) {
                    console.log("未使用", name.toString())
                    path.remove()
                } else {
                    console.log("已使用", name.toString(), path.parentPath.parent.type)
                }
            }
        } else {
            return undefined
        }
    },
};


traverse(ast, opts = Fun)

const code_ = generate(ast, opts = {jsescOption: {"minimal": true}})
// console.log()
// console.log(code_.code)
fs.writeFile('./demo.js', code_.code, (err => {}))